package com.ddx.controller;


import com.ddx.common.exception.ExceptionUtil;
import com.ddx.common.exception.ResultModel;
import com.ddx.common.util.DataUtil;
import com.ddx.dto.ShortVideoDto;
import com.ddx.dto.UserDto;
import com.ddx.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author dudianbo
 * @ClassName: UserController
 * @Description: ${DESCRIPTION}
 * @date 2018/4/18
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping(value = "/app")
public class UserLoginController {
    private static Logger logger = LoggerFactory.getLogger(UserLoginController.class);
    @Autowired
    IUserService iUserService;

    @RequestMapping(value = "/user/login", method = RequestMethod.POST)
    public ResultModel login(@RequestBody UserDto userDto, HttpServletRequest request) {
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            userDto.setDevicecId(request.getHeader("deviceId"));
            Map<String, String> map = iUserService.login(userDto);
            resultModel.setData(map);
        } catch (Exception e) {
            logger.error("登录异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }

    /**
     * 验证码登录
     *
     * @param userDto
     * @param request
     * @return
     */
    @RequestMapping(value = "/user/smsLogin", method = RequestMethod.POST)
    public ResultModel smsLogin(@RequestBody UserDto userDto, HttpServletRequest request) {
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            userDto.setDevicecId(request.getHeader("deviceId"));
            userDto.setChannel(request.getHeader("channel"));
            String registerIp = DataUtil.getIpAdrress(request);
//            userDto.setRegisterIp(registerIp);
            Map<String, String> map = iUserService.smsLogin(userDto);
            resultModel.setData(map);
        } catch (Exception e) {
            logger.error("登录异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }

    /**
     * 用户注册接口
     *
     * @param userDto
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/user/register", method = RequestMethod.POST)
    public ResultModel register(@RequestBody UserDto userDto, HttpServletRequest request) {
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            String registerIp = DataUtil.getIpAdrress(request);
//            userDto.setRegisterIp(registerIp);
            userDto.setChannel(request.getHeader("channel"));
            Map<String, String> map = iUserService.register(userDto);
            resultModel.setData(map);
        } catch (Exception e) {
            logger.error("注册异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }

//    /**
//     * 发送验证码
//     *
//     * @param appAccountDto
//     * @return
//     */
//    @CrossOrigin(origins = "*", maxAge = 3600)
//    @RequestMapping(value = "/user/sendSms", method = RequestMethod.POST)
//    public ResultModel sendSms(@RequestBody AppAccountDto appAccountDto, HttpServletRequest request) {
//        ResultModel resultModel = ResultModel.getSuccessInstance();
//        try {
//            iAppAccountService.sendSms(appAccountDto);
//        } catch (Exception e) {
//            logger.error("发送验证码异常", e);
//            resultModel = ExceptionUtil.getMessageException(e);
//        }
//        return resultModel;
//    }


//
//    /**
//     * 验证短信
//     *
//     * @param appAccountDto
//     * @return
//     */
//    @RequestMapping(value = "/user/verifySmsCode", method = RequestMethod.POST)
//    public ResultModel verifySmsCode(@RequestBody AppAccountDto appAccountDto, HttpServletRequest request) {
//        ResultModel resultModel = ResultModel.getSuccessInstance();
//        try {
//            String key = iAppAccountService.verifySmsCode(appAccountDto);
//            Map<String, String> map = new HashMap<>();
//            map.put("key", key);
//            resultModel.setData(map);
//        } catch (Exception e) {
//            logger.error("短信验证接口异常", e);
//            resultModel = ExceptionUtil.getMessageException(e);
//        }
//        return resultModel;
//    }

    /**
     * 密码重置接口
     *
     * @param userDto
     * @return
     */
    @RequestMapping(value = "/user/resetPsd", method = RequestMethod.POST)
    public ResultModel resetPsd(@RequestBody UserDto userDto, HttpServletRequest request) {
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            iUserService.resetPsd(userDto);
        } catch (Exception e) {
            logger.error("密码重置接口异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    /**
     * 第三方登录接口
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/user/thirtLogin", method = RequestMethod.POST)
    public ResultModel thirtLogin(@RequestBody Map<String,Object> map, HttpServletRequest request) {
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            Map<String,String> result=iUserService.thirtLogin(map);
            resultModel.setData(result);
        } catch (Exception e) {
            logger.error("第三方登录接口异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    /**
     * 关于我们接口
     *
     * @return
     */
    @RequestMapping(value = "/aboutUs", method = RequestMethod.POST)
    public ResultModel aboutUs(HttpServletRequest request) {
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("isQQ", 0);
            map.put("isWeixin", 0);
            map.put("isDianbao", 1);
            resultModel.setData(map);
        } catch (Exception e) {
            logger.error("关于我们接口异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
}
